package com.aiss.ws.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.ExamItem;
import com.aiss.ws.R;
import com.aiss.ws.adapter.AnswerAdatper;
import com.aiss.ws.adapter.AnswerSucessAdatper;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang on 16/5/17.
 */
@EActivity(R.layout.activity_answersucess)
public class AnswerSucessActivity extends BaseActivity {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==1){
//                Intent intent = new Intent();
//                intent.putExtra("position",msg.arg1);
//                setResult(4,intent);
//                finish();
            }else if(msg.what==Command.HANDLECODE14){

                showMessage("错题已提交、请在错题中查看");
                float d = (float) num/item.getTestItems().size()*100;
                fraction_tv.setText(String.format("%.2f",d)+"%");
                adapter = new AnswerSucessAdatper(item.getTestItems(),context,handler);
                gridView.setAdapter(adapter);
                fraction_ll.setVisibility(View.VISIBLE);
                cencer_ll.setVisibility(View.VISIBLE);
            }

        }
    };

    private Context context  = this;

    private int num = 0;

    private StringBuilder sb = new StringBuilder();

    /**
     * 分数
     */
    @ViewById(R.id.fraction_tv)
    TextView fraction_tv;

    @ViewById(R.id.cencer_ll)
    LinearLayout cencer_ll;

    /**
     * 分数布局
     */
    @ViewById(R.id.fraction_ll)
    LinearLayout fraction_ll;

    @ViewById(R.id.title)
    TextView title;

    @ViewById(R.id.next)
    TextView title_tv;

    @Click(R.id.back)
    void back() {
        finish();
    }

    @ViewById(R.id.gridView)
    GridView gridView;

    @Extra
    ExamItem item;

    @Extra
    String idx;

    private AnswerSucessAdatper adapter;

    @AfterViews
    void init(){
        title_tv.setText("交卷成功");

        for(int position = 0 ; position < item.getTestItems().size(); position++){
            if(item.getTestItems().get(position).getType().equals("单选题")||item.getTestItems().get(position).getType().equals("判断题")){
                if(item.getTestItems().get(position).getMe_answer()!=null){
                    if(Command.ANSWER[Integer.parseInt(item.getTestItems().get(position).getMe_answer())].equals(item.getTestItems().get(position).getAnswer())){
                        ++num;
                    }else{
                        sb.append(item.getTestItems().get(position).getIdx()+",");
                    }
                }else{
                    sb.append(item.getTestItems().get(position).getIdx()+",");
                }


            }else{//多选题

                if(item.getTestItems().get(position).getMe_more_answer()!=null&&item.getTestItems().get(position).getMe_more_answer().size()!=0){
                    String xn = "";
                    for(int i = 0 ; i < item.getTestItems().get(position).getMe_more_answer().size() ; i++){
                        xn += Command.ANSWER[Integer.parseInt(item.getTestItems().get(position).getMe_more_answer().get(i))];
                    }

                    if(xn.equals(item.getTestItems().get(position).getAnswer())){
                        ++num;
                    }else{
                        sb.append(item.getTestItems().get(position).getIdx()+",");
                    }


                }else{
                    sb.append(item.getTestItems().get(position).getIdx()+",");
                }

            }
        }

        HashMap<String,String> map = new HashMap<>();
        map.put("type","subresult");
        map.put("idx",idx);
        map.put("wrongs",sb.toString());
        Futil.xutils(Url.EXAM,map,handler,Command.HANDLECODE14,context);
    }




}
